<template>
  <div class style="height: 100%;padding: 20px;">
    <div :id="uniqueID" style="height: 100%;width: 100%;"></div>
  </div>
</template>
<script>
import echarts from "echarts";
import uuidv1 from "uuid/v1";
import defaultmixins from "@/mixins/bigScreen/defaultmixins";
import axios from "axios";

export default {
  name: "chartlinecharthour",
  mixins: [defaultmixins],
  components: {},
  data() {
    return {
      uniqueID: ""
    };
  },
  computed: {},
  watch: {},
  created() {
    this.uniqueID = uuidv1();
  },
  mounted() {
    this.loadData();
  },
  methods: {
    async loadData() {
      let scale = this.$root.setting.scale;

      var date = new Date();
      var month = date.getMonth() + 1;
      var datetime =
        date.getFullYear() +
        "" + // "年"
        (month >= 10 ? month : "0" + month) +
        "" + // "月"
        (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) +
        "" +
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) +
        "" +
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        "" +
        (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());

      let form = new FormData();
      form.append("showapi_timestamp", datetime);
      form.append("showapi_appid", 27292);
      form.append("showapi_sign", "829aecc822384203adc70d2c4853f5e3");
      form.append("area", "郑州");
      form.append("areaid", "101180101");

      let res = await axios.post("http://route.showapi.com/9-8", form);
      let hourForecastData = [];
      let ress = res.data;
      if (res.data.showapi_res_code == 0) {
        //返回成功
        var hourList = res.data.showapi_res_body.hourList;
        for (var i in hourList) {
          var time = hourList[i].time;

          if (time == undefined) {
            continue;
          }
          var year = time.substring(0, 4); //不包含最后一个字符
          var month = time.substring(4, 6);
          var day = time.substring(6, 8);
          var hour = time.substring(8, 10);
          var minute = time.substring(10, 12);
          var date = hour + ":" + minute;
          if (hour == "00" && minute == "00") {
            date = year + "-" + month + "-" + day + " " + hour + ":" + minute;
          }
          hourForecastData.push([date, Number(hourList[i].temperature)]);
        }
      }

      var hourOption = {
        // backgroundColor: '#0e2147',
        title: {
          text: "未来24小时天气预报(℃)",
          textStyle: {
            color: "#fff",
            fontSize: 36 * scale,
            fontWeight: "bold"
          }
        },
        tooltip: {
          show: true,
          trigger: "axis",
          // formatter: "{a} {b}: {c} ℃",
          textStyle: {
            fontWeight: "normal",
            fontSize: 30 * scale
          }
        },
        grid: {
          left: "5%",
          right: "5%",
          bottom: "5%",
          top: "20%",
          containLabel: true
        },
        legend: {
          show: false, //【2019年11月7日】不展示legend
          type: "scroll",
          // data: Line,
          itemWidth: 18,
          itemHeight: 12,
          textStyle: {
            color: "#F5F5F5",
            fontSize: 30 * scale
          }
        },
        xAxis: [
          {
            //x轴
            type: "category", //数据类型为不连续数据
            boundaryGap: false, //坐标轴两边是否留白
            axisLine: {
              show: true,
              onZero: false, //【2019年12月5日】X 轴或者 Y 轴的轴线是否在另一个轴的 0 刻度上，只有在另一个轴为数值轴且包含 0 刻度时有效。
              lineStyle: {
                color: "#F5F5F5"
              }
            },
            axisLabel: {
              inside: false,
              textStyle: {
                color: "#fff",
                fontWeight: "normal",
                fontSize: 30 * scale
                // lineHeight: 22
              }
              // formatter: function (value, index) {
              //     var year = value.substring(0,4);//不包含最后一个字符
              //     var month = value.substring(4,6);
              //     var day = value.substring(6,8);
              //     var hour = value.substring(8,10);
              //     var minute = value.substring(10,12);

              //     return hour+":"+minute;
              // }
            },
            axisTick: { show: false } //刻度点数轴
            // data:xData,
            // data: ['20:00', '22:00', '00:00', '2:00', '4:00', '6:00', '8:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00'],
          }
        ],
        yAxis: [
          {
            //y轴的相关设置
            type: "value", //y轴数据类型为连续的数据
            // name:"单位:℃",
            splitLine: {
              //y轴上的y轴线条相关设置
              show: false
            },
            axisLine: {
              //y轴的相关设置
              show: true,
              lineStyle: {
                color: "#F5F5F5" //y轴颜色
              }
            },
            axisLabel: {
              textStyle: {
                color: "#ffffff",
                fontSize: 30 * scale
              }
            },
            axisTick: { show: false } //刻度点数轴
          }
        ],
        series: [
          {
            name: "温度",
            type: "line", //统计图类型为折线图
            smooth: true, //是否平滑曲线显示
            symbolSize: 0, //数据点的大小，[0,0]//b表示宽度和高度
            lineStyle: {
              //线条的相关设置
              normal: {
                color: "#53f3f3" // 线条颜色
              }
            },
            itemStyle: {
              normal: {
                borderWidth: 5,
                color: "#53f3f3"
              }
            },
            areaStyle: {
              //区域填充样式
              normal: {
                //线性渐变，前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’，则该四个值是绝对像素位置。
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    { offset: 0, color: "rgba(9,96,152,0.8)" },
                    { offset: 1, color: "rgba(9,96,152,0.06)" } //9,96,152
                  ],
                  false
                ),
                shadowColor: "rgba(9,96,152, 0.9)", //阴影颜色
                shadowBlur: 20 //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
              }
            },
            data: hourForecastData
          }
        ]
      };

      var weatherEchartHour = echarts.init(
        document.getElementById(this.uniqueID)
      );
      weatherEchartHour.setOption(hourOption);
      window.addEventListener("resize", () => {
        weatherEchartHour.resize();
      });
    }
  }
};
</script>
<style lang='scss' scoped>
</style>